﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using FineUI;
using SDLITS.Common;
using SDLITS.Model;
using MySoft.Data;
namespace SDLITS.Web.Basic
{
    public partial class RoomChange : CommonBase
    {
        #region 页面初始化
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDrp();
                LoadData();
            }
        }
        #endregion
        protected void BindDrp()
        {
            List<Area> lists = SDDbSession.Default.From<Area>().ToList();
            drp_area.DataSource = lists;
            drp_area.DataTextField = "AreaName";
            drp_area.DataValueField = "AreaID";
            drp_area.DataBind();
        }
        #region 读取数据


        private void LoadData()
        {
            int id = 0;
            if (!string.IsNullOrEmpty(Request["id"]))
            {
                id = int.Parse(Request["id"]);
            }
            if (id > 0)
            {
                Room Tm = SDDbSession.Default.From<Room>().Where(Room._.RoomID == id).ToFirst();
                txt_Name.Text = Tm.RoomName;
                txt_Description.Text = Tm.Description;
                txt_IP.Text = Tm.LedIP;
                txt_No.Text = Tm.RoomNo;
                txt_Rent.Text = Tm.RentalPrice.ToString();
                txt_ScreenNo.Text = Tm.ScreenNo;
                txt_sqmeters.Text = Tm.SqMeters.ToString();
                drp_area.SelectedValue = Tm.AreaID.ToString();
            }
        }
        #endregion

        #region 保存数据
        protected void btnSaveRefresh_Click(object sender, EventArgs e)
        {
            int returnvalue = 0;
            Room Tm = new Room();
            if (!string.IsNullOrEmpty(Request["id"]))
            {
                Tm.RoomID = int.Parse(Request["id"]);
                Tm.Attach();
                Tm.RoomName = txt_Name.Text;
                Tm.Description = txt_Description.Text;
                Tm.AreaID = int.Parse(drp_area.SelectedValue);
                Tm.LedIP = txt_IP.Text;
                Tm.RentalPrice = string.IsNullOrEmpty(txt_Rent.Text) ? 0 : decimal.Parse(txt_Rent.Text);
                Tm.RoomNo = txt_No.Text;
                Tm.ScreenNo = txt_ScreenNo.Text;
                Tm.SqMeters = string.IsNullOrEmpty(txt_sqmeters.Text) ? 0 : decimal.Parse(txt_sqmeters.Text);              
                returnvalue = SDDbSession.Default.Update<Room>(Tm);
            }
            else
            {
                ModelLoginUser user = Session["LoginUser"] as ModelLoginUser;
                Tm.DeAttach();
                Tm.RoomName = txt_Name.Text;
                Tm.Description = txt_Description.Text;
                Tm.AreaID = int.Parse(drp_area.SelectedValue);
                Tm.LedIP = txt_IP.Text;
                Tm.RentalPrice = string.IsNullOrEmpty(txt_Rent.Text) ? 0 : decimal.Parse(txt_Rent.Text);
                Tm.RoomNo = txt_No.Text;
                Tm.ScreenNo = txt_ScreenNo.Text;
                Tm.SqMeters = string.IsNullOrEmpty(txt_sqmeters.Text) ? 0 : decimal.Parse(txt_sqmeters.Text);  
                Tm.StatusID =1;
                Tm.RoomTypeID = 1;
                Tm.CreateDate = DateTime.Now;
                Tm.CreateUserID = user.UserID;
                returnvalue = SDDbSession.Default.Insert<Room>(Tm);
            }
            if (returnvalue > 0)
            {
                PageContext.RegisterStartupScript(ActiveWindow.GetHideRefreshReference());
            }
        }
        #endregion
    }
}